package cn.sumpu.app.guoguojie.biz.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.sumpu.app.guoguojie.biz.AppEventBiz;
import cn.sumpu.app.guoguojie.dao.AppEventDao;
import cn.sumpu.app.guoguojie.domain.AppEvent;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;
import cn.sumpu.app.guoguojie.util.Common;
import cn.sumpu.app.guoguojie.util.IdGenerator;

public class AppEventBizImpl implements AppEventBiz{

	private AppEventDao appEventDao;
	
	public void setAppEventDao(AppEventDao appEventDao) {
		this.appEventDao = appEventDao;
	}

	public AppEvent insert(AppEvent appEvent) throws AppBusinessException {
		appEvent.setIdentifier(IdGenerator.createIdentifier());
		appEventDao.insert(appEvent);
		return appEvent;
	}

	@Override
	public void update(AppEvent appEvent) throws AppBusinessException {
		appEventDao.update(appEvent);
		
	}

	@Override
	public List<AppEvent> getAppEventByFamily(String family)
			throws AppBusinessException {
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("family", family);
		return appEventDao.getAppEvent(map);
	}
	

	@Override
	public List<AppEvent> getAppEventByDay(String family,String day)
			throws AppBusinessException {
		if(Common.empty(day)) return null;
		String queryStartDay = day+" "+"00:00:00";
		String queryEndDay = day+" "+"23:59:59";
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("family", family);
		map.put("queryStartDay", queryStartDay);
		map.put("queryEndDay", queryEndDay);
		
		map.put("startId", 0);
		map.put("pageSize", 3);
		return appEventDao.getAppEvent(map);
	}
	@Override
	public AppEvent getAppEventLatestNow(String family)throws AppBusinessException {
		Date date = new Date();
		String day = Common.getFormatDate(date, "yyyy-MM-dd");
		String queryStartDay = Common.getFormatDate(date, "yyyy-MM-dd HH:mm:ss");
		String queryEndDay = day+" "+"23:59:59";
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("family", family);
		map.put("queryStartDay", queryStartDay);
		map.put("queryEndDay", queryEndDay);
		
		map.put("startId", 0);
		map.put("pageSize", 1);
		List<AppEvent> eventList = appEventDao.getAppEvent(map);
		if(eventList!=null && eventList.size()>0){
			return eventList.get(0);
		}else{
			return null;
		}
	}
	
	
	

	@Override
	public AppEvent getAppEventLatestFutre(String family)
			throws AppBusinessException {
		Date date = new Date();
		String day = Common.getFormatDate(date, "yyyy-MM-dd");
		String queryStartDay = Common.addDate(day+" "+"00:00:00", 1);
		String queryEndDay = Common.addDate(day+" "+"23:59:59", 15);
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("family", family);
		map.put("queryStartDay", queryStartDay);
		map.put("queryEndDay", queryEndDay);
		
		map.put("startId", 0);
		map.put("pageSize", 1);
		List<AppEvent> eventList = appEventDao.getAppEvent(map);
		if(eventList!=null && eventList.size()>0){
			return eventList.get(0);
		}else{
			return null;
		}
	}

	@Override
	public int getMainAppEventCount(String family) throws AppBusinessException {
		String nowDay = Common.getFormatDate(new Date(), "yyyy-MM-dd");
		String queryStartDay = Common.addDate(nowDay+" "+"00:00:00", 1);
		String queryEndDay = Common.addDate(nowDay+" "+"23:59:59", 15);
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("family", family);
		map.put("queryStartDay", queryStartDay);
		map.put("queryEndDay", queryEndDay);
		return appEventDao.getAppEventCount(map);
	}

	@Override
	public AppEvent getAppEventById(String identifier)
			throws AppBusinessException {
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("identifier", identifier);
		return appEventDao.getAppEvent(map).get(0);
	}

	@Override
	public void deleteById(String identifier) throws AppBusinessException {
		appEventDao.deleteById(identifier);
		
	}
	

}
